#include <iostream>
#include "MsgInstanceClient.h"
#include "Communication.h"
#include "string.h"
#include "time.h"
#include <signal.h>
#include "EventLoop.h"
#include "HistoryRecord.h"
#include "Hotplug.h"
#include "ComLog.h"
#include "SerialCommunication.h"
#include "MsgInstanceServer.h"
#include <pthread.h>
#include "ComTimer.h"
#include "AT_CmdBase.h"
#include <time.h>

char tembufrd[1024] ={"OK\r\n"};
char tembufwd[1024] ={0};
int at_cmd_init()
{


    return 0;
}

int AT_SendData_t(char *senddata, int datalen)
{
    COMLOG_ERROR_LOG("send : %s\n", senddata);
    memcpy(tembufwd, senddata, datalen);
    return datalen;
}


int  AT_RevData_t(char *revbuf, int wantsize)
{
     wantsize = strlen(tembufrd) > wantsize ? wantsize : strlen(tembufrd);
     memcpy(revbuf, tembufrd, wantsize);
   //  memset(tembufrd, 0, sizeof(tembufrd));
     return wantsize;
}

time_t getticks()
{
    struct timeval tv_begin;
    gettimeofday(&tv_begin, NULL);
    return tv_begin.tv_sec*1000+tv_begin.tv_usec/1000000;
}

void cb(char *reponsedata, AT_CMD_STATU cmdstatus, void *data)
{
    static int i = 0;
    COMLOG_ERROR_LOG(">>>> %s %d %d\n", reponsedata, cmdstatus, i++);

}

int main(void)
{
    AT_CmdBaseRegister(at_cmd_init, AT_SendData_t, AT_RevData_t, getticks);
   // for(int i = 0; i < AT_CMD_QUEUE_MAX_SIZE; i++)
    {
    if(AT_CmdBaseSendWithConfirmReply("AT+OK\r\n", "OK\r\n", "ERROR", cb, 200, NULL)==-1)
        printf("register cmd error\n");
    if(AT_CmdBaseSendWithConfirmReply("AT+OK1\r\n", "OK1\r\n", "ERROR1", cb, 200, NULL)==-1)
        printf("register cmd error\n");  
    if(AT_CmdBaseSendWithUnConfirmReply("AT+OK2\r\n", cb, 1000, NULL)==-1)
        printf("register cmd error\n");       
    }  
   // AT_SendData_t("OK\r\n", strlen("OK\r\n"));
    while(1)
    {
        AT_CmdBaseProcess();
        usleep(10*1000);
    }

    return 0;
}